﻿#include "msg_clientdata.h"
#include <QtEndian>
#include <QDateTime>
#include <complex>

char f [4]; float v ;

float  Msg_ClientData::last_data[156];
int  Msg_ClientData::_frame = 0;
Msg_ClientData::Msg_ClientData(QObject* parent)
	: Msg{ parent }
{
	if (_frame == 0)
	{//memset(last_data, 0, sizeof(last_data));
		for (int i = 0; i < 156; i++)
		{
			last_data[i] = qrand() % 1000;
		}
	}


    f[0] = 0;
    f[1] = 0;
    f[2] = 0;
    f[3] = 5;

  //    v = *(float*) f;
    v = 5;
}

int Msg_ClientData::Parse(QByteArray dat, int dataLen)
{
	memcpy(&Head0, dat.data(), dataLen);

	memcpy(value, dat.data() + 2, dataLen - 4);
   // value = qFromBigEndian(value);
	memcpy(&Checksum, dat.data() + 626, 2);


	return dataLen;
}


QByteArray Msg_ClientData::ToByte()
{

	msg_data.clear();

	msg_data.resize(628);

	msg_data[0] = Head0;
	msg_data[1] = _frame++;

	float* p = (float*)&msg_data.data()[2];
	for (int i = 0; i < 156; i++, p++)
	{
		//	last_data[i] += (qrand() % 10000) / 100.f;
		//	last_data[i] /= 2;
        //last_data[i] = 40.0f * sin(QTime::currentTime().msec()s / 10.0f);

        *p =(qrand() % 40  + 40 )  *sin((last_data[i] + QTime::currentTime().msecsSinceStartOfDay()) * i/ 100000.0f);
	}

	calcChecksum();

	memcpy(msg_data.data() + 626, &Checksum, 2);


	return msg_data;
}
